import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from matplotlib.ticker import PercentFormatter

# 设置学术图表风格
plt.rcParams.update({
    'font.family': 'serif',
    'font.serif': ['Times New Roman'],
    'font.size': 12,
    'axes.labelsize': 14,
    'axes.titlesize': 16,
    'legend.fontsize': 12,
    'xtick.labelsize': 12,
    'ytick.labelsize': 12,
    'figure.figsize': (10, 6),
    'figure.dpi': 300,
    'axes.grid': True,
    'grid.linestyle': '--',
    'grid.alpha': 0.3
})
sns.set_style("whitegrid")


theta_values = np.array([0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.10])
accuracy = np.array([98.5, 98.3, 98.2, 98.0, 97.9, 97.7, 97.5, 94.5, 93.8])  # 准确率(%)
comm_gain = np.array([35, 42, 50, 62, 58, 55, 52, 68, 70])  # 通信增益(%)

# 创建图表和双坐标轴
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

# 绘制准确率曲线（左侧Y轴）
accuracy_line, = ax1.plot(theta_values, accuracy, 
                         color='#3498DB', marker='o', markersize=8, 
                         linewidth=3, label='Accuracy')

# 绘制通信增益曲线（右侧Y轴）
gain_line, = ax2.plot(theta_values, comm_gain, 
                     color='#E74C3C', marker='s', markersize=8, 
                     linewidth=3, linestyle='--', label='Comm Gain')

# 标注最优阈值点 (θ=0.05)
# ax1.annotate('Optimal Threshold\nθ=0.05', 
#              xy=(0.05, 98.0), xytext=(0.06, 97.0),
#              arrowprops=dict(arrowstyle='->', lw=1.5, color='darkgreen'),
#              bbox=dict(boxstyle="round,pad=0.3", fc="#ABEBC6", ec="#27AE60", alpha=0.8))

# # 标注极端场景点
# ax1.annotate('Over-Pruning: θ=0.10\nAccuracy↓4.2%', 
#              xy=(0.10, 93.8), xytext=(0.08, 92.0),
#              arrowprops=dict(arrowstyle='->', lw=1.5, color='darkred'),
#              bbox=dict(boxstyle="round,pad=0.3", fc="#FADBD8", ec="#E74C3C", alpha=0.8))

# ax1.annotate('Under-Pruning: θ=0.02\nComm Gain↓35%', 
#              xy=(0.02, 98.5), xytext=(0.03, 99.0),
#              arrowprops=dict(arrowstyle='->', lw=1.5, color='darkblue'),
#              bbox=dict(boxstyle="round,pad=0.3", fc="#D6EAF8", ec="#3498DB", alpha=0.8))

# 添加关键性能指标
# ax1.text(0.11, 96.5, r'$\theta=0.05$: Balanced Trade-off', 
#          fontsize=12, bbox=dict(facecolor='lightyellow', alpha=0.8))
# ax1.text(0.11, 95.5, r'Accuracy Loss: $\downarrow$0.8%', 
#          fontsize=11, color='#3498DB')
# ax1.text(0.11, 94.5, r'Comm Gain: $\uparrow$62%', 
#          fontsize=11, color='#E74C3C')

# 设置坐标轴范围
ax1.set_ylim(90, 100)
ax2.set_ylim(30, 80)

# 设置坐标轴标签
ax1.set_xlabel(r'Pruning Threshold $\theta_{stable}$', fontweight='bold')
ax1.set_ylabel('Accuracy (%)', fontweight='bold', color='#3498DB')
ax2.set_ylabel('Communication Gain (%)', fontweight='bold', color='#E74C3C')

# 设置刻度颜色匹配曲线
ax1.tick_params(axis='y', colors='#3498DB')
ax2.tick_params(axis='y', colors='#E74C3C')

# 添加标题
# ax1.set_title('Impact of Dynamic Pruning Threshold on Performance', 
#               fontweight='bold', pad=20)

# 添加图例
lines = [accuracy_line, gain_line]
ax1.legend(lines, [l.get_label() for l in lines], 
           loc='upper center', bbox_to_anchor=(0.5, -0.15),
           ncol=2, frameon=True, framealpha=0.9)

# 添加网格线
ax1.grid(True, linestyle='--', alpha=0.3)
ax2.grid(False)

# 学术图表规范调整
plt.tight_layout()
plt.subplots_adjust(bottom=0.2)
fig.patch.set_facecolor('white')

# 保存为高分辨率图片
plt.savefig('Fig3_Pruning_Threshold_Impact.png', dpi=300, bbox_inches='tight')
plt.show()
